function project = align_project(project)

% ALIGN_PROJECT  Ignore the supplied translation and scale for each image,
%   and find these using a least-squares problem on the point constraints
%   instead.

for i = 1:length(project.images)
    image = project.images(i);
    K = length(image.constraints3d);
    
    if K == 0
        continue;
    end
    
    pts = zeros(K, 3);
    for k = 1:K
        e = project.mesh.verts(image.constraints3d(k)).edge.next;
        pts(k, :) = project.mesh.limitevaluation(e, 0, 0) * project.vertices;
    end
    rotated_pts = (image.rotate(1:3, 1:3) * pts')';
    [x y s] = align_rotated(image.constraints2d, rotated_pts(:, 1:2));
    
    image.translate        = eye(4);
    image.translate(1, 4)  = x;
    image.translate(2, 4)  = y;
    image.scale            = s * eye(4);
    image.scale(4, 4)      = 1;
    image.transform        = image.translate * image.rotate * image.scale;
    
    project.images(i) = image;
end

end
